//
//  SecurityStatusObject.m
//  LocomotiveGuards
//
//  Created by apple on 15/11/17.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "SecurityStatusObject.h"
#import "PopLoadMsg.h"
#import "PopMsg.h"

@implementation SecurityStatusObject

- (instancetype)init
{
    if (self = [super init])
    {
        self.currentAlarmStatus = AlarmStateNull;
        self.abNormalStatus     = SecurityAbnormalStatusNormal;
        self.lowPowerStatus     = SecurityLowPowerStatusNormal;
        self.cutStatus          = SecurityCutStatusNormal;
        self.fenceStatus        = SecurityElecFenceStatusNormal;
        [self getAlarmConfig];
    }
    return self;
}

- (void) dealloc
{
    NSLog(@"释放对象%@",[self class]);
}
/**
 *  从服务器查询一键防盗告警配置
 */
- (void) getAlarmConfig
{
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    NSString * urlMethod = [NSString stringWithFormat:@"%@getAlarmConfig.do",urlstring];
    NSDictionary * paramDict = @{@"bid":bid};
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         NSDictionary * responseDict = responseObject;
         NSNumber * flag = [responseDict objectForKey:@"flag"];
         if (flag.longValue == 0 )
         {
             if ([[responseDict objectForKey:@"status"] isEqualToString:@"0"])
             {
                 [_delegate setAlarmConfigView:AlarmStateOn];
                 self.currentAlarmStatus = AlarmStateOn;
                 [self getAlarmStatus];
             }
             else if ([[responseDict objectForKey:@"status"] isEqualToString:@"1"])
             {
                 [_delegate setAlarmConfigView:AlarmStateOff];
                 self.currentAlarmStatus = AlarmStateOff;
             }
             else
             {
                 
             }
         }
         else
         {
             NSLog(@"获取一键防护状态失败！");
             [PopMsg showMessage:@"获取一键防护状态失败" Lasttime:2];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2];
     }];
}

/**
 *  一键防护开启/关闭
 *
 *  @param _state <#_state description#>
 */
- (void) setAlarmConfig:(AlarmState) _state
{
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    NSMutableDictionary * requestDict = [[NSMutableDictionary alloc]init];
    
    [requestDict setObject:bid forKey:@"bid"];
    [requestDict setObject:_state==AlarmStateOn?@"0":@"1" forKey:@"status"];
    
    NSString * urlMethod = [NSString stringWithFormat:@"%@setAlarmConfig.do",urlstring];
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam:requestDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         NSDictionary * responseDict = responseObject;
         NSNumber * flag = [responseObject objectForKey:@"flag"];
         if (0 == flag.longValue )
         {
             self.currentAlarmStatus = _state;
             [self.delegate setAlarmConfigView:_state];
         }
         else if ( -1 == flag.longValue )
         {
             NSLog(@"%@",responseDict[@"msg"]);
         }
         else
         {
             NSLog(@"服务器返回出错！");
             [PopMsg showMessage:@"服务器返回出错" Lasttime:2];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2];
     }];
}

/**
 *  获取设备当前的告警状态信息
 */
- (void) getAlarmStatus
{
    if ( AlarmStateOn != self.currentAlarmStatus )
    {
        return;
    }
    
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    
    NSString * urlMethod = [NSString stringWithFormat:@"%@getAlarmStatus.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":loginId,@"bid":bid};
    
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         NSDictionary * responseDict = responseObject;
         NSString * value = nil;
         NSNumber * flag = nil;
         
         flag  = [responseDict objectForKey:@"flag"];
         if (!flag)
         {
             NSLog(@"查询防护状态失败！");
             [PopMsg showMessage:@"查询防护状态失败！" Lasttime:2];
         }
        
         
         //NSLog(@"qqqqqqqqq  %@",responseDict);
         if ( 0 == flag.longValue )
         {
             value = [responseDict objectForKey:@"abnormalStatus"];
             if (value)
             {
                 self.abNormalStatus = value.intValue;
                 [_delegate setSecurityAbnormalStatusView:SecurityStateYellow SecurityAbnormalStatus:self.abNormalStatus];
                 value = nil;
             }
             else
             {
                 NSLog(@"服务器返回异常状态失败！");
                 [PopMsg showMessage:@"服务器返回异常状态失败！" Lasttime:2];
             }
             
             
             value = [responseDict objectForKey:@"lowPowerStatus"];
             if (value)
             {
                 self.lowPowerStatus = value.intValue;
                 [_delegate setSecurityLowPowerStatusView:SecurityStateYellow SecurityLowPowerStatus:self.lowPowerStatus];
                 value = nil;
             }
             else
             {
                 NSLog(@"服务器返回低电状态失败！");
                 [PopMsg showMessage:@"服务器返回低电状态失败！" Lasttime:2];
             }
             
             value = [responseDict objectForKey:@"cutStatus"];
             if (value)
             {
                 self.cutStatus = value.intValue;
                 [_delegate setSecurityCutStatusView:SecurityStateYellow SecurityCutStatus:self.cutStatus];
                 value = nil;
             }
             else
             {
                 NSLog(@"服务器返回切断状态失败！");
                 [PopMsg showMessage:@"服务器返回切断状态失败！" Lasttime:2];
             }
             
             value = [responseDict objectForKey:@"elecFenceStatus"];
             if (value)
             {
                 self.fenceStatus = value.intValue;
                 [_delegate setSecurityElecFenceStatusView:SecurityStateYellow SecurityElecFenceStatus:self.fenceStatus];
                 value = nil;
             }
             else
             {
                 NSLog(@"服务器返回电子围栏状态失败！");
                 [PopMsg showMessage:@"服务器返回电子围栏状态失败！" Lasttime:2];
             }
         }
         else
         {
             NSLog(@"获取一键防护状态失败！");
             [PopMsg showMessage:@"获取一键防护状态失败" Lasttime:2];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2];
         /*
            清状态
          */
         [_delegate clearSecurityStatusView];
     }];
}

/**
 *  查询异常状态
 *
 */
- (void) getSecurityAbnormalStatus
{
    if ( AlarmStateOn != self.currentAlarmStatus )
    {
        NSLog(@"请开启一键防护");
        [PopMsg showMessage:@"请开启一键防护" Lasttime:2.0];
        return;
    }
    
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    NSString * urlMethod = [NSString stringWithFormat:@"%@getAlarmStatus.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":loginId,@"bid":bid};
    
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];

    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         NSDictionary * responseDict = responseObject;
         NSString * value = nil;
         NSNumber * flag = nil;
         
         flag  = [responseDict objectForKey:@"flag"];
         if (!flag)
         {
             [PopMsg showMessage:@"查询异常状态失败" Lasttime:2.0];
         }
         
         if ( 0 == flag.longValue )
         {
             value = [responseDict objectForKey:@"abnormalStatus"];
             if (value)
             {
                 self.abNormalStatus = value.intValue;
                 [_delegate setSecurityAbnormalStatusView:SecurityStateBlue SecurityAbnormalStatus:self.abNormalStatus];
                 value = nil;
             }
             else
             {
                 [PopMsg showMessage:@"查询异常状态失败" Lasttime:2.0];
             }
         }
         else
         {
             [PopMsg showMessage:@"查询异常状态失败" Lasttime:2.0];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2.0];
     }];
}

/**
 *  查询低电报警状态
 *
 */
- (void) getSecurityLowPowerStatus
{
    if ( AlarmStateOn != self.currentAlarmStatus )
    {
        NSLog(@"请开启一键防护");
        [PopMsg showMessage:@"请开启一键防护" Lasttime:2.0];
        return;
    }
    
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    
    NSString * urlMethod = [NSString stringWithFormat:@"%@getAlarmStatus.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":loginId,@"bid":bid};
    
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         NSDictionary * responseDict = responseObject;
         NSString * value = nil;
         NSNumber * flag = nil;
         
         flag  = [responseDict objectForKey:@"flag"];
         if (!flag)
         {
             [PopMsg showMessage:@"查询低电报警状态失败" Lasttime:2];
         }
         if ( 0 == flag.longValue )
         {
             value = [responseDict objectForKey:@"lowPowerStatus"];
             if (value)
             {
                 self.lowPowerStatus = value.intValue;
                 [_delegate setSecurityLowPowerStatusView:SecurityStateBlue SecurityLowPowerStatus:self.lowPowerStatus];
                 value = nil;
             }
             else
             {
                 [PopMsg showMessage:@"查询低电报警状态失败" Lasttime:2];
             }
         }
         else
         {
             [PopMsg showMessage:@"查询低电报警状态失败" Lasttime:2];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"查询防护状态失败！");
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2];
     }];
}

/**
 *  查询切断状态
 *
 */
- (void) getSecurityCutStatus
{
    if ( AlarmStateOn != self.currentAlarmStatus )
    {
        NSLog(@"请开启一键防护");
        [PopMsg showMessage:@"请开启一键防护" Lasttime:2.0];
        return;
    }
    
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    NSString * urlMethod = [NSString stringWithFormat:@"%@getAlarmStatus.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":loginId,@"bid":bid};
    
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         NSDictionary * responseDict = responseObject;
         NSString * value = nil;
         NSNumber * flag = nil;
         
         flag  = [responseDict objectForKey:@"flag"];
         if (!flag)
         {
             [PopMsg showMessage:@"查询切断状态失败" Lasttime:2];
         }
         if ( 0 == flag.longValue )
         {
             value = [responseDict objectForKey:@"cutStatus"];
             if (value)
             {
                 self.cutStatus = value.intValue;
                 [_delegate setSecurityCutStatusView:SecurityStateBlue SecurityCutStatus:self.cutStatus];
                 value = nil;
             }
             else
             {
                  [PopMsg showMessage:@"查询切断状态失败" Lasttime:2];
             }
         }
         else
         {
             [PopMsg showMessage:@"查询切断状态失败" Lasttime:2];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2];
     }];
}

/**
 *  获取电子围栏状态
 *
 */
- (void) getSecurityElecFenceStatus
{
    if ( AlarmStateOn != self.currentAlarmStatus )
    {
        NSLog(@"请开启一键防护");
        [PopMsg showMessage:@"请开启一键防护" Lasttime:2.0];
        return;
    }
    
    NSString * loginId = [[NSUserDefaults standardUserDefaults]objectForKey:@"phonenumber"];
    NSString * bid     = [[NSUserDefaults standardUserDefaults]objectForKey:@"bid"];
    NSString * urlMethod = [NSString stringWithFormat:@"%@getAlarmStatus.do",urlstring];
    NSDictionary * paramDict = @{@"loginId":loginId,@"bid":bid};
    
    PopLoadMsg *loadmsg=[PopLoadMsg new];
    [loadmsg delayLoadImages];
    
    [K_AppDelegate GenerateHTTPRequestWithUrl:urlMethod withParam: paramDict success:
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         [loadmsg delayImagesRemove];
         
         NSDictionary * responseDict = responseObject;
         NSString * value = nil;
         NSNumber * flag = nil;
         
         flag  = [responseDict objectForKey:@"flag"];
         if (!flag)
         {
             [PopMsg showMessage:@"查询电子围栏状态失败" Lasttime:2];
         }
         if ( 0 == flag.longValue )
         {
             value = [responseDict objectForKey:@"elecFenceStatus"];
             if (value)
             {
                 self.fenceStatus = value.intValue;
                 [_delegate setSecurityElecFenceStatusView:SecurityStateBlue SecurityElecFenceStatus:self.fenceStatus];
                 value = nil;
             }
             else
             {
                 [PopMsg showMessage:@"查询电子围栏状态失败" Lasttime:2];
             }
         }
         else
         {
             [PopMsg showMessage:@"查询电子围栏状态失败" Lasttime:2];
         }
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         [loadmsg delayImagesRemove];
         [PopMsg showMessage:@"网络连接失败" Lasttime:2];
     }];
}

@end
